mod appellation;
mod selectorion;

use appellation::Appellation as Appellation;
use selectorion::Selector as Selector;
use std::net::Ipv4Addr;


fn main() {
    println!("Hello, world!");
    let _a = Appellation {name: "测1".to_string(), nicknames: vec![String::from("测2"), String::from("测3")]};
    // a.add_nickname("Dom");
    // a.add_nickname("Coke");
    // println!("a.name = {}", a.name);
    // println!("a.nicknames = {:?}", a.nicknames);
    println!("end to a");
    let _a =  Appellation {name: "测4".to_string(), nicknames: vec![String::from("测5"), String::from("测6")]};
    // a.add_nickname("IT");
    // a.add_nickname("Book");
    // println!("a.name = {}", a.name);
    // println!("a.nicknames = {:?}", a.nicknames);
    // drop(a)
    println!("end of main");

    let mut s = Selector {
        elements: vec!['x', 'y', 'z'],
        current: 2,
    };
    assert_eq!(*s, 'z');
    println!("s = {}", s.is_alphabetic());

    *s = 'w';

    assert_eq!(s.elements, ['x', 'y', 'w']);

    let s = Selector {
        elements: vec!["good", "bad", "ugly"],
        current: 1,
    };

    show_it(&s);
    // show_it_generic(&s);
    show_it_generic(&s as &str);
    // *表示dereference 解引用
    show_it_generic(&*s);

    println!("ping1 {:?}", ping(Ipv4Addr::new(23,21,68,141)));
    println!("ping2 {:?}", ping([8,8,8,8]));
    println!("ping3 {:?}", ping(0xd076eb94_u32));

}

fn show_it(thing:&str) {
    println!("{}", thing);
}

fn show_it_generic<T: std::fmt::Display>(thing: T) {
    println!("{}", thing);
}

fn ping<A>(address: A) -> std::io::Result<bool> 
where A: Into<Ipv4Addr> {
    let ipv4 = address.into();
    let response = std::process::Command::new("ping")
        .arg(ipv4.to_string())
        .arg("-c1")
        .arg("-W1")
        .stdout(std::process::Stdio::inherit())
        .output()?;
    Ok(response.status.success())
}